package com.leigang.java.code.mulithread;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.*;

/**
 * @author leigang
 * @version 1.0
 * @date 2023/5/30 21:28
 **/
public class MuliThead {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //创建线程池
        ExecutorService service = Executors.newFixedThreadPool(10);
        //提交任务，并用 Future提交返回结果
        List<Integer> integerList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Future<Integer> future = service.submit(new CallableTask());
            Integer integer = future.get();
            integerList.add(integer);
        }
        System.out.println(integerList);

    }

    static class CallableTask implements Callable<Integer> {

        @Override
        public Integer call() throws Exception {
            return new Random().nextInt();
        }

    }
}
